﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class Form1 : Form
    {
        private const string rpf = "奥智科技专属报表文件|*.azrpf";
        public Form1()
        {
            InitializeComponent();
            rpfDesignerControl1.Variables=new Hashtable()
            {
                {"小票抬头","杭州奥智科技"}
            };
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var ofd = new SaveFileDialog();
            ofd.Filter = rpf;
            if (ofd.ShowDialog()==DialogResult.OK)
            {
                rpfDesignerControl1.SaveAsDocument(ofd.FileName);
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = rpf;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                rpfDesignerControl1.LoadDocument(ofd.FileName);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                var ofd = new OpenFileDialog();
                ofd.Filter = rpf;
                if (ofd.ShowDialog()!= DialogResult.OK)
                {
                    return;
                }

                ReportPrinting.ReportDocument report_document = new ReportPrinting.ReportDocument();
                report_document.Body = null;
                report_document.PageFooter = null;
                report_document.PageFooterMaxHeight = 0F;
                report_document.PageHeader = null;
                report_document.PageHeaderMaxHeight = 0F;
                report_document.ResetAfterPrint = true;

                ReportPrintingFramework.RPFReportMaker report_maker = new ReportPrintingFramework.RPFReportMaker();

               
                // Add sample variables
                // You can change variable names and number according to your report needs
                Hashtable variables = new Hashtable();
                variables["Sample var 1"] = "var1 value";
                variables["Sample var 2"] = "var2 value";
                variables["Sample var 3"] = "var3 value";
                variables["QRCODE"] = DateTime.Now.ToString();
                report_maker.Variables = variables;

                report_maker.Load(ofd.FileName);

                report_document.DefaultPageSettings.Landscape = report_maker.RPFDocument.Landscape;
                report_document.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(
                    (int)(float)(report_maker.RPFDocument.Margins.Left * 100.0f),
                    (int)(float)(report_maker.RPFDocument.Margins.Right * 100.0f),
                    (int)(float)(report_maker.RPFDocument.Margins.Top * 100.0f),
                    (int)(float)(report_maker.RPFDocument.Margins.Bottom * 100.0f));
                report_document.ReportMaker = report_maker;

                this.m_printDialog.PrinterSettings = (PrinterSettings)report_maker.RPFDocument.PrinterSettings.Clone();
                report_document.PrinterSettings = this.m_printDialog.PrinterSettings;

                this.m_printDialog.Document = report_document;
                if (this.m_printDialog.ShowDialog(this) != DialogResult.OK)
                    return;
                this.m_printDialog.Document.Print();
            }
            catch (Exception excp)
            {
                MessageBox.Show(this, excp.ToString());
            }
        }
    }
}
